<template>
  <div class="register-container">
    <div class="form-box">
      <h2 class="form-title">用户注册</h2>
      <form @submit.prevent="registerUser">
        <div class="form-group">
          <label for="username" class="form-label">用户名</label>
          <input type="text" class="form-control" id="username" v-model="username" required />
        </div>
        <div class="form-group">
          <label for="password" class="form-label">密码</label>
          <input type="password" class="form-control" id="password" v-model="password" required />
        </div>
        <div class="form-group">
          <label for="email" class="form-label">邮箱</label>
          <input type="email" class="form-control" id="email" v-model="email" required />
        </div>
        <div class="form-group">
          <label for="region" class="form-label">地区</label>
          <input type="text" class="form-control" id="region" v-model="region" required />
        </div>
        <div class="form-group">
          <label for="role" class="form-label">角色</label>
          <select class="form-control" v-model="role" required>
            <option value="VISITOR">来访者</option>
            <option value="CONSULTANT">咨询师</option>
          </select>
        </div>
        <div v-if="role === 'CONSULTANT'" class="form-group">
          <label for="file" class="form-label">上传资质证明</label>
          <input type="file" class="form-control" id="file" @change="handleFileChange" />
        </div>
        <button type="submit" class="btn btn-primary">注册</button>
      </form>
      <p v-if="errorMessage" class="text-danger">{{ errorMessage }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      email: '',
      region: '',
      role: 'VISITOR', // 默认角色为来访者
      file: null,
      errorMessage: '',
    };
  },
  methods: {
    handleFileChange(event) {
      this.file = event.target.files[0];
    },
    async registerUser() {
      const formData = new FormData();
      formData.append('username', this.username);
      formData.append('password', this.password);
      formData.append('email', this.email);
      formData.append('region', this.region);
      formData.append('role', this.role);
      if (this.role === 'CONSULTANT' && this.file) {
        formData.append('file', this.file);
      }

      try {
        const response = await this.$axios.post('/user/register', formData, {
          headers: { 'Content-Type': 'multipart/form-data' },
        });
        if (response.status === 200) {
          this.$router.push("/login");  // 注册成功后跳转
        } else {
          console.error("注册失败", response);
        }
      } catch (error) {
        this.errorMessage = error.response ? error.response.data : '注册失败，请重试';
      }
    },
  },
};
</script>

<style scoped>
/* 背景渐变色 */
.register-container {
  background: linear-gradient(135deg, #87CEFA, #ffb6c1); /* 渐变背景色 */
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 注册表单容器 */
.form-box {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  padding: 30px;
  width: 100%;
  max-width: 400px;
}

/* 表单标题 */
.form-title {
  text-align: center;
  font-size: 1.8rem;
  color: #4e73df; /* 深蓝色 */
  margin-bottom: 20px;
}

/* 输入框和选择框样式 */
.form-group {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.form-group label {
  font-size: 1rem;
  font-weight: bold;
  margin-right: 15px; /* 给标签和输入框之间增加间距 */
  width: 100px; /* 标签的宽度固定，左对齐 */
}

.form-control {
  width: 100%;
  padding: 12px 15px;
  border-radius: 8px;
  border: 1px solid #ddd;
  background-color: #f8f9fa;
  font-size: 1rem;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-control:focus {
  border-color: #4e73df;
  outline: none;
  box-shadow: 0 0 8px rgba(72, 123, 234, 0.5);
}

/* 选择框 */
select.form-control {
  padding: 12px 15px;
  border-radius: 8px;
  background-color: #f8f9fa;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

select.form-control:focus {
  border-color: #4e73df;
  outline: none;
  box-shadow: 0 0 8px rgba(72, 123, 234, 0.5);
}

/* 按钮样式 */
button {
  background-color: #4e73df;
  color: white;
  padding: 12px;
  border-radius: 10px;
  border: none;
  width: 100%;
  cursor: pointer;
  font-size: 1.1rem;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #2e59d9;
}

/* 错误消息 */
.text-danger {
  color: red;
  text-align: center;
  margin-top: 15px;
  font-size: 1rem;
}
</style>
